import * as mobx from 'mobx';
import { ListStore } from './listStore'
import { WeaTools,WeaLocaleProvider } from 'ecCom';
import * as API_RECYCLE from '../apis/recycle';

const observable = mobx.observable;
const action = mobx.action;
const Base64 = WeaTools.Base64;
const getLabel = WeaLocaleProvider.getLabel;
export class WFRecycleStore extends ListStore{
    @observable nowRouterWfpath = "listDoing";
    @observable showBatchSubmit = false;
    @observable phrasesObj = {};
    @observable baseParams = {
        method: "all",
        offical: "",
        officalType: -1
    };
    @observable searchParams = {
        viewcondition:0
    };  
    nowViewType = 2;

    constructor() {
        super();
        this.appendBaseParams({
            viewScope: "delete",
            complete: 0
        });
        
        this.initDatas = this.initDatas.bind(this);
        this.doSearch = this.doSearch.bind(this);
        this.appendBaseParams = this.appendBaseParams.bind(this);
        this.clearStatus = this.clearStatus.bind(this);
        this.setConditionByTree = this.setConditionByTree.bind(this);
        this.leftTree = WeaTools.ls.getJSONObj(`listRecycleLeftTree`) || [];
    }

    @action
    appendBaseParams(params) {
        this.baseParams = {...this.baseParams,...params};
        if(params.tabTitle){
            this.title = Base64.decode(decodeURIComponent(params.tabTitle));
        }
    }
    initDatas(params = {}){
        const method = this.searchParams.method;
        API_RECYCLE.getRecycleRequestList({ ...this.baseParams, actiontype: "baseinfo" }).then((data) => {
            const { treedata , conditioninfo , countcfg , treecount , groupinfo , totalcount} = data;
            WeaTools.ls.set(`listRecycleLeftTree`, treedata);
            this.condition = conditioninfo;
            this.leftTree = treedata;
            this.leftTreeCountType = countcfg;
            this.leftTreeCount = treecount;
            //树节点没有数据时，右侧列表自动显示所有数据
            if(this.selectedTreeKey!="" && !treecount.hasOwnProperty(this.selectedTreeKey)){
                this.setShowSearchAd(false);
                this.setSelectedTreeKey("");
                this.clearFormFields();
                this.initDatas({method:"all"});
                this.doSearch({
                    method: 'all',
                    viewcondition: 0
                });
            }
            //
            // this.topTab = groupinfo;
            // this.topTabCount = (method === "reqeustbytype" || method === "reqeustbywfid") ? treecount[this.selectedTreeKey] : totalcount;
            this.form.initFormFields(conditioninfo);
        });
    }

    doSearch(params = {}){
        //获取表单的参数值
        const searchParamsAd = this.form.getFormParams();
		const newParams = { ...this.baseParams, ...this.searchParams, ...searchParamsAd, ...params, actiontype: "splitpage" };
        this.loading = true;
        if(newParams.method === "reqeustbytype" || newParams.method === "reqeustbywfid")
            this.topTabCount = this.leftTreeCount[this.selectedTreeKey];
        API_RECYCLE.getRecycleRequestList(newParams).then((data) => {
            this.tableStore.getDatas(data.sessionkey, params.current || 1);
            this.searchParams = { ...this.baseParams, ...this.searchParams, ...params };
            this.sharearg = data.sharearg;
            this.loading = false;
		});
    }
    setConditionByTree(){
        const key = this.selectedTreeKey;
        if(!key)
            return;
        //选中树节点同步修改高级查询条件
        let formFields = {};
        if(key.indexOf("wf_") === 0){
            const workflowid = key.substring(3);
            let workflowname = '';
            this.leftTree && this.leftTree.map(t =>{
                t.childs && t.childs.map(w =>{
                    if(w.domid === key)
                        workflowname = w.name;
                });
            });
            formFields["workflowid"] = {value:workflowid, valueSpan:workflowname};
        }else if(key.indexOf("type_") === 0){
            const workflowtype = key.substring(5);
            let typename = '';
            this.leftTree && this.leftTree.map(t =>{
                if(t.domid === key)
                    typename = t.name;
            });
            formFields["workflowtype"] = {value:workflowtype, valueSpan:typename};
        }
        this.setFormFields(formFields);
    }
    clearStatus(){
        this.showSearchAd = false;
        this.selectedTreeKey = "";
        this.searchParams = {viewcondition:0};
        this.title = getLabel(129343,'流程回收站');
        this.clearFormFields();
    }
    setNowViewType = (v) => {
        this.nowViewType = v;
    }
}